package com.dc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dc.common.ResponseResultVO;
import com.dc.entity.ro.DeviceRO;
import com.dc.feign.DeviceApiServiceFeign;
import com.dc.mapper.DeviceHistoryLogMapper;
import com.dc.model.Device;
import com.dc.model.DeviceHistoryLog;
import com.dc.service.DeviceHistoryLogService;
import com.dc.service.DeviceService;
import com.dc.util.ExportUtil;
import com.dc.util.SnowflakeIdWorker;
import com.dc.util.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

@Service
public class DeviceHistoryLogServiceImpl extends ServiceImpl<DeviceHistoryLogMapper, DeviceHistoryLog> implements DeviceHistoryLogService {

    @Resource
    private DeviceHistoryLogMapper deviceHistoryLogMapper;

    @Override
    public ResponseResultVO selectDeviceByPage(DeviceRO ro) {
        Page page = new Page<Map<String, Object>>(ro.getCurrentPage(), ro.getPageSize());
        QueryWrapper<DeviceHistoryLog> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().in(CollectionUtils.isNotEmpty(ro.getIds()), DeviceHistoryLog::getId, ro.getIds());
        queryWrapper.lambda().like(StringUtils.isNotBlank(ro.getDeviceAddress()), DeviceHistoryLog::getDeviceAddress, ro.getDeviceAddress());
        queryWrapper.lambda().eq(StringUtils.isNotBlank(ro.getSerialNumber()), DeviceHistoryLog::getSerialNumber, ro.getSerialNumber());
        queryWrapper.lambda().like(StringUtils.isNotBlank(ro.getDeviceFactory()), DeviceHistoryLog::getDeviceFactory, ro.getDeviceFactory());
        queryWrapper.lambda().eq(null != ro.getDeviceStatus(), DeviceHistoryLog::getDeviceStatus, ro.getDeviceStatus());
        queryWrapper.lambda().eq(null != ro.getSubSystem(), DeviceHistoryLog::getSubSystem, ro.getSubSystem());

        queryWrapper.lambda().eq(StringUtils.isNotBlank(ro.getFloorCode()), DeviceHistoryLog::getFloorCode, ro.getFloorCode());
        queryWrapper.lambda().eq(StringUtils.isNotBlank(ro.getDeviceType()), DeviceHistoryLog::getDeviceType, ro.getDeviceType());
        queryWrapper.lambda().ge(StringUtils.isNotBlank(ro.getAlarmTimeStart()), DeviceHistoryLog::getAlarmTime, ro.getAlarmTimeStart());
        queryWrapper.lambda().le(StringUtils.isNotBlank(ro.getAlarmTimeEnd()), DeviceHistoryLog::getAlarmTime, ro.getAlarmTimeEnd());

        queryWrapper.lambda().ge(StringUtils.isNotBlank(ro.getStartTime()), DeviceHistoryLog::getCreateTime, ro.getStartTime());
        queryWrapper.lambda().le(StringUtils.isNotBlank(ro.getEndTime()), DeviceHistoryLog::getCreateTime, ro.getEndTime());

        queryWrapper.lambda().orderByDesc(DeviceHistoryLog::getUpdateTime);
        return ResponseResultVO.resultList(deviceHistoryLogMapper.selectPage(page, queryWrapper), ro);
    }

    /**
     * 插入
     *
     * @param ro
     * @return
     */
    @Override
    public ResponseResultVO insertDevice(DeviceRO ro) {
        DeviceHistoryLog device = new DeviceHistoryLog();
        BeanUtils.copyProperties(ro, device);
        device.setId(SnowflakeIdWorker.generateId());
        return ResponseResultVO.insertResult(deviceHistoryLogMapper.insert(device));
    }

    @Override
    public ResponseResultVO insertDevice(List<Device> list) {
        List<DeviceHistoryLog> logList = new LinkedList<>();
        for (Device device : list) {
            DeviceHistoryLog log = new DeviceHistoryLog();
            BeanUtils.copyProperties(device, log);
            log.setId(SnowflakeIdWorker.generateId());
            logList.add(log);
        }
        return ResponseResultVO.successResult(this.saveBatch(logList));
    }

    /**
     * 导出
     *
     * @param response
     * @param ro
     */
    @Override
    public void deviceRecordExport(HttpServletResponse response, DeviceRO ro) {
        ResponseResultVO roadReportForm = this.selectDeviceByPage(ro);
        HashMap<String, Object> data = (HashMap<String, Object>) roadReportForm.getData();
        ExportUtil.exportExcelNew("设备运行记录", (List) data.get("list"), Device.class, response);
    }
}
